// var url = 'http://localhost';
var url = 'http://ycs.acocomoon.com';
layui.use(['layer'],function(){
    var layer = layui.layer;
    $(document).ajaxStart(function(){
        layer.load(1,{
            shade: [0.3,'#fff'] //0.1透明度的白色背景
        })
    });
    $(document).ajaxError(function(event,xhr,options,exc){
        layer.open({
            title: '异常',
            content:'数据请求异常',
            area:'420px',
            btn:['刷新','关闭'],
            resize:false,
            yes:function(){
                if (xhr.status == 40000){//登录超时
                    top.location.href ='./login.html'
                }else{
                    location.reload();
                }
            }
        });
    });
    $(document).ajaxComplete(function(){
        layer.closeAll('loading');
    });

    $(document).on({
        keyup : function(e){
            var flag = e.target.isNeedPrevent;
            if(flag)  return;
            search();
            e.target.keyEvent = false ;

        },
        keydown : function(e){
            e.target.keyEvent = true ;
        },
        input : function(e){
            if(!e.target.keyEvent){
                search();
            }
        },
        compositionstart : function(e){
            e.target.isNeedPrevent = true ;
        },
        compositionend : function(e){
            e.target.isNeedPrevent = false;

        }
    },'input[name=search]');
});
function permission(){
	var auto=$('[auth]');
	var permission=$.parseJSON(localStorage.getItem('permission'));
	for(var k=0;k<auto.length;k++){
		if(permission.indexOf($(auto[k]).attr('auth'))==-1){
			$(auto[k]).remove();
		}else{
            $(auto[k]).show();
        }
	}
}
function unlogin(title, r) {
	if(r.responseJSON==undefined){
		layui.use('layer', function() {
			var layer = layui.layer;
			layer.open({
				type: 1,
				title: '登陆超时',
				area:["40%",'30%'],
				content: `<p class='one_p'>网络请求错误!</p>`,
				btn: ['关闭'],
				skin: 'my-skin'
			})
		});
		setTimeout(function(){
			localStorage.clear();
			window.top.location = './login.html';
		},2000);
	}else{
		if(r.responseJSON.code==100001){
			layui.use('layer', function() {
				var layer = layui.layer;
				layer.open({
					type: 1,
					title: '登陆超时',
					area:["40%",'30%'],
					content: `<p class='one_p'>${r.responseJSON.msg}</p>`,
					btn: ['关闭'],
					skin: 'my-skin'
				})
			});
			setTimeout(function(){
				localStorage.clear();
				window.top.location = './login.html';
			},2000)
		}else{
			layui.use('layer', function() {
				var layer = layui.layer;
				layer.open({
					type: 1,
					title: title,
					area:["40%",'30%'],
					content: `<p class='one_p'>${r.responseJSON.msg}</p>`,
					btn: ['关闭'],
					skin: 'my-skin'
				})
			});
		}
	}
}

//日期
function fomDate(time){
	var date=new Date();
	var yyyy=date.getFullYear();//四位的年份
	var mm=Number(date.getMonth())+1;//月份
	var dd=date.getDate();//当前日
	var HH=date.getHours();//小时
	var MM=date.getMinutes();//分钟
	var SS=date.getSeconds();//秒
	if(mm.toString().length==1){
		mm='0'+mm;
	}
	if(dd.toString().length==1){
		dd='0'+dd;
	}
	if(MM.toString().length==1){
		MM='0'+MM;
	}
	if(SS.toString().length==1){
		SS='0'+SS;
	}
	if(time=='yyyy-mm-dd'){
		return yyyy+'-'+mm+'-'+dd;
	}else if(time='yyyy-mm-dd HH:MM:SS'){
		return yyyy+'-'+mm+'-'+dd+' '+HH+':'+MM+':'+SS;
	}
}

/*
 * 取token
 */
function getToken(){
    return $.parseJSON(localStorage.getItem('login_msg')).token;
}

/*
 * ajax操作提示
 */
function tips(tipsName,r){
    if(r.msg==""){
        r.msg='操作成功!'
    }
    layer.open({
        title:tipsName,
        content: r.msg,
        area:'420px',
        offset: '15rem',
        btn:['关闭']
    });
}

/*
 * @name 项目下拉列表
 * @return {string}
 */
function projectSelect(){
    var option = '<option value="0">-</option>';
    $.ajax({
        url:url+'/project/project/selectLists',
        type: "POST",
        async: false,
        data: {
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            }else{
                tips('项目下拉列表',r);
            }
        }
    });
    return option;
}

/*
 * @name 楼栋下拉列表
 * @return {string}
 */
function areaSelect(projectId){
    var option = '<option value="">请选择施工区</option>';
    $.ajax({
        url:url+'/project/project/areaSelectLists',
        type: "POST",
        async: false,
        data: {
            projectId:projectId,
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            }else{
                tips('楼栋下拉列表',r);
            }
        }
    });
    return option;
}
/*
 * @name 楼层下拉列表
 * @return {string}
 */
function sectionSelect(areaId){
    var option = '<option value="">请选择施工段</option>';
    $.ajax({
        url:url+'/project/project/sectionSelectLists',
        type: "POST",
        async: false,
        data: {
            areaId:areaId,
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            }else{
                tips('楼层下拉列表',r);
            }
        }
    });
    return option;
}
/*
 * @name 工种下拉列表
 * @return {string}
 */
function professionSelect(){
    var option = '<option value="">请选择工种</option>';
    $.ajax({
        url:url+'/setting/profession/lists',
        type: "POST",
        async: false,
        data: {
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.data.length; i++) {
                    option += `
                        <option value=${r.data.data[i].id}>${r.data.data[i].name}</option>
                    `
                }
            }else{
                tips('工种下拉列表',r);
            }
        }
    });
    return option;
}
/*
 * @name 施工项下拉列表
 * @return {string}
 */
function assignmentSelect(id){
    var option = '<option value="">请选择施工项</option>';
    $.ajax({
        url:url+'/setting/assignment/lists',
        type: "POST",
        async: false,
        data: {
            professionId:id,
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.data.length; i++) {
                    option += `
                        <option value=${r.data.data[i].id}>${r.data.data[i].name}</option>
                    `
                }
            }else{
                tips('施工项下拉列表',r);
            }
        }
    });
    return option;
}

/*
 * @name 班组下拉列表
 * @return {string}
 */
function groupSelect(projectId = '',professionId = ''){
    var option = '<option value="">-</option>';
    $.ajax({
        url:url+'/hrm/group/selectLists',
        type: "POST",
        async: false,
        data: {
            projectId:projectId,
            professionId:professionId=== ''?'':Number(professionId),
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id} data-projectid="${r.data[i].projectId}">${r.data[i].name}</option>
                    `
                }
            }else{
                tips('班组下拉列表',r);
            }
        }
    });
    return option;
}

/**
 *
 */
function yearSelect(){
    var option = '<option value="">-</option>';
    $.ajax({
        url:url+'/setting/year/lists',
        type: "POST",
        async: false,
        data: {
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            }else{
                tips('工种下拉列表',r);
            }
        }
    });
    return option;
}

/**
 *
 */
function supplierSelect(isNew = false){
    var option = '<option value="">-</option>';
    $.ajax({
        url:url+'/hrm/supplier/selectLists',
        type: "POST",
        async: false,
        data: {
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}>${r.data[i].name}</option>
                    `
                }
            }else{
                tips('供应商下拉列表',r);
            }
        }
    });
    if(isNew){
        option +='<option value="0">请输入新的供应商</option>';
    }
    return option;
}

/**
 *
 */
function materialSelect(){
    var option = '<option value="">-</option>';
    $.ajax({
        url:url+'/setting/material/selectLists',
        type: "POST",
        async: false,
        data: {
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `
                        <option value=${r.data[i].id}  data-unitid="${r.data[i].unitId}" data-unitname="${r.data[i].unitName}">${r.data[i].name}</option>
                    `
                }
            }else{
                tips('材料下拉列表',r);
            }
        }
    });
    return option;
}

/**
 *
 */
function warehouseSelect(){
    var option = '<option value="">-</option>';
    $.ajax({
        url:url+'/warehouse/warehouse/materialLists',
        type: "POST",
        async: false,
        data: {
            projectId:getParam('projectId'),
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    spec = r.data[i].spec?r.data[i].spec:'';
                    option += `
                        <option value=${r.data[i].materialId} data-id="${r.data[i].id}">${r.data[i].materialName}</option>
                    `
                }
            }else{
                tips('仓库库存下拉列表',r);
            }
        }
    });
    return option;
}

/**
 *
 */
function specSelect(id){
    var option = '<option value="" data-unitid="" data-unitname="">-</option>';
    $.ajax({
        url:url+'/setting/material/specSelectLists',
        type: "POST",
        async: false,
        data: {
            materialId:id,
            token:getToken()
        },
        success:function(r){
            if(r.code==100000){
                for(var i = 0; i < r.data.length; i++) {
                    option += `<option value=${r.data[i].id} data-unitid="${r.data[i].unitId}" data-unitname="${r.data[i].unitName}">${r.data[i].spec}</option>`
                }
            }else{
                tips('规格下拉列表',r);
            }
        }
    });
    return option;
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function getParam(param){
    var login_msg = JSON.parse(localStorage.getItem('login_msg'));
    return eval('login_msg.'+param);
}

function search(){
    var val=$('input[name=search]').val();
    var this_ind=$('input[name=search]');
    var this_next=$('input[name=search]').next();
    $.ajax({
        type:"post",
        url:url+'/hrm/employee/search',
        beforeSend:function(){
            $('input[name=search]').attr('data-id','');
            $('input[name=search]').attr('data-number','');
        },
        async:false,
        data:{
            projectId:getParam('projectId'),
            search:val,
            token:getParam('token')
        },
        success:function(r){
            this_next.show();
            if(r.data==""||r.data==null){
                this_next.html('未查找到此人');
            }else{
                var li='';
                for(var i=0;i<r.data.length;i++){
                    li+=`<li data-id=${r.data[i].id} data-number="${r.data[i].jobNumber}">${r.data[i].name}</li>`
                }
                this_next.html(li);
                $('ul li').click(function(){
                    this_ind.val($(this).html());
                    this_ind.data('id',$(this).data('id'));
                    this_ind.data('number',$(this).data('number'));
                    $('ul').hide();
                })
            }
        }
    })
}

function approvalStatus(status){
    switch(status){
        case 0:
            return '待审核';
        case 1:
            return '审核中';
        case 2:
            return '已通过';
        case 3:
            return '未通过';
    }
}

/**
 * 价格、纯数字格式整理
 * @param p
 * @param style
 * @returns {string}
 */
function numberFormat(p,style){
    switch(style){
        case 'currency':
            option = { style: 'currency', currency: 'CNY' };
            break;
        default:
            option = { minimumFractionDigits:0,maximumFractionDigits:4};
    }
    return Number(p).toLocaleString('zh-hans-CN', option)
}